﻿
//--> Loth 2011 
//--> Complete Interface v0.3

package chamans
{
	import com.greensock.*;
	import com.greensock.easing.*;
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.utils.*;
	
	public class Interface extends Sprite
	{
		public function Interface(B:*, Final:Function=null, Def:Object=null):void
		{
			FIN=Final || new Function();
			BASE=B;
			def=Def;
			addEventListener(Event.ADDED_TO_STAGE,onStage);
		}
		public function onStage(e:Event) : void 
		{	
			removeEventListener(Event.ADDED_TO_STAGE,onStage);
			ID=this.parent.loaderInfo.parameters.id || -1;
			if(ID!=-1)ExternalInterface.addCallback("TRANSPHERE", TRANSPHERE);
			Define(def);
			content=new Sprite();
			addChild (content);
			FONT=new SYS_fonts("assets/interface.css", "assets/fonts.swf", LoadClipInterface, ID)
		}
		public function Define(Def:Object) : void 
		{
			BASE.LOAD.OUT=OUT;
			BASE.LOAD.ID=ID;
			if (!Def)Def=new Object();
			MESSAGE = Def.message || " ";
		}
		public function TRANSPHERE(x:int, y:int) : void 
		{
			if(BASE.limite) { 
				BASE.sizelimit=new Point(x,y);
				BASE.activeResize(true)
			}}
		public function LoadClipInterface() : void {
			BASE.LOAD.SWF("assets/interface.swf",["visor", "zone", "bug"], CreateInterface ) // loader
		}
		public function CreateInterface(A:Array) : void 
		{
			Inter=A;
			// /!\ menuconfig XML
			MENUXML=new SYS_xml();
			// config panel
			PANNEL=new SYS_pannel(BASE,FONT);
			// top menu text
			MENUclip=new Sprite();
			MENUtext=new SYS_text( FONT.CSS, 380, 5, 2 );
			MENUtext.readXML( MENUXML.BASE, MENUaction );
			// top menu interne text
			MENUintern=new SYS_text( FONT.CSS );
			// text debug bottom
			SORTIEtext=new SYS_text(FONT.CSS, 350, 5, 18);  
			if(MESSAGE) {OUT(MESSAGE+"mess");BASE.LOAD.BaseTexte=MESSAGE}
			coinBas=new Sprite();
			clipBas=new Sprite();
			content.addChild (clipBas);
			clipBas.addChild (SORTIEtext);
			content.addChild (coinBas);
			content.addChild (MENUclip);
			content.addChild (MENUtext);
			cadre(new Point(parent.stage.stageWidth, parent.stage.stageHeight)); 
			//	MODE(MENUintern); MODE(FULLtext);  MODE(FULLtext2); MODE(MENUtext); MODE(SORTIEtext);
			FPS();
			actif=true;
			TestFlashPlayer()
		}
		public function MODE(m:*):void
		{
			m.filters=[new DropShadowFilter(0, 45, 0x000000, .8, 2, 2, 3, 1)]
		}
		private function TestFlashPlayer():void
		{
			var Ver:Array = Capabilities.version.split(' ')[1].split(',');
			var Version:String = Ver[0]+"."+Ver[1]+"."+Ver[2]+"."+Ver[3];
			var F:Array=BASE.FlashLimiteVersion;
			if(Number(Version.slice(0,2))>=F[0] && Number(Version.slice(3,4))>=F[2] && Ver[2]!=F[3]) FIN();
			else if (Number(Version.slice(0,2))>=11) FIN();
			else { BUG=new PlayerError(BASE, Version, new Inter[2]());  addChildAt(BUG,0);
			}}
		
		// ________________________________message text
		public function OUT(t:String):void{
			if (!SORTIEtext) return;
			var txt:XML=<xml><![CDATA[<sortie></sortie>]]></xml>;
			txt.sortie=t;
			SORTIEtext.readXML(txt);
		}
		//___________________________________________Visor fps
		public function VISOR(actif:Boolean=false):void
		{
			if(!viseur){cible=new Inter[0]();
				viseur=new Sprite()
				viseur.addChild(cible);
				viseur.x=BASE.size.x>>1; viseur.y=BASE.size.y>>1;
				content.addChild(viseur);}
			if(actif){
				ZONE= new Inter[1]();
				viseur.addChild(ZONE);
				cible.visible=false;
				ZONE.iner.buttonMode=true;
				cible.ci.visible=false;
				add_MessagePlay();
				active_cible();
			}
			else{if(viseur){
				viseur.removeChild(cible);
				content.removeChild(viseur);
				viseur=null
			}}}
		public function active_cible():void
		{
			ZONE.iner.N.addEventListener(MouseEvent.MOUSE_OVER, Vover)
			ZONE.iner.N.addEventListener(MouseEvent.MOUSE_OUT, Vout)
			ZONE.iner.N.addEventListener(MouseEvent.MOUSE_UP, Vclick)
		}
		public function cible_retour(b:Boolean=true):void{if(cible)
		{
			if(b){cible.ci.visible=true; cible.ci.play() }
			else {cible.ci.visible=false; cible.ci.gotoAndStop(1)}
		}}
		public function cible_walk(b:Boolean=true):void{if(cible)
		{
			if(b){cible.iner.gotoAndStop(2); }
			else {cible.iner.gotoAndStop(1); }
		}}
		private function  Vover(e:MouseEvent):void
		{
			ZONE.iner.gotoAndPlay(2)
			read_MPlay(1)
		}
		private function  Vout(e:MouseEvent):void
		{
			ZONE.iner.gotoAndStop(1)
			read_MPlay(0)
		}
		private function  Vclick(e:MouseEvent):void
		{
			read_MPlay(2)
			ZONE.iner.N.removeEventListener(MouseEvent.MOUSE_OVER, Vover)
			ZONE.iner.N.removeEventListener(MouseEvent.MOUSE_OUT, Vout)
			ZONE.iner.N.removeEventListener(MouseEvent.MOUSE_UP, Vclick)
			ZONE.iner.gotoAndPlay(21);
			viseur.removeChild(MessagePlay)
			//BASE.Pause(add_MessagePlayTmp)
			var time:Timer = new Timer(500, 1 );
			time.start();
			time.addEventListener(TimerEvent.TIMER_COMPLETE, add_MessagePlayTmp);
		}
		private function add_MessagePlayTmp(e:Event=null):void
		{ 
			viseur.removeChild(ZONE);
			viseur.mouseChildren=false;
			viseur.mouseEnabled=false;
			cible.visible=true;
			BASE.autoMove=true;
			//Special Bullet demo
			if(BASE.physicEngine=="jiglib" || BASE.physicEngine=="bullet")BASE.physics.activeGUN(true);
			
		}
		private function add_MessagePlay():void
		{  
			MessagePlay=new SYS_text(FONT.CSS, 300,-150, -10);
			viseur.addChild(MessagePlay);
			read_MPlay()
		}
		private function read_MPlay(n:int=0):void
		{  var m:XML
			if(n==0)m=<xml><bouton1>ENTER</bouton1></xml>;
			if(n==1)m=<xml><bouton2>ENTER</bouton2></xml>;
			if(n==2)m=<xml><bouton3>ENTER</bouton3></xml>;
			MessagePlay.setHTML(m.toString());
		}
		//_____________________________________________ menu action definition
		private function MENUaction(t:String):void
		{			
			/*switch (t){ // loader version
			case "PLUS": MENUintern.loadXML("xml/m_plus.xml", MENUactionINTERN); break; 
			case "DISPLAY": MENUintern.loadXML("xml/m_display.xml", MENUactionINTERN); break; 
			case "OPTION": MENUintern.loadXML("xml/m_option.xml", MENUactionINTERN); break; 
			case "ABOUT": MENUintern.loadXML("xml/m_about.xml", MENUactionINTERN); break; 
			}*/
			if(newMenu!=null)hideMENU()
			switch (t){ // direct version
				case "PLUS": MENUintern.readXML(MENUXML["PLUS"+Plusnum], MENUactionINTERN); break; 
				case "DISPLAY": MENUintern.readXML(MENUXML["DISPLAY"+Display], MENUactionINTERN);break; 
				case "PROJECT": MENUintern.readXML(MENUXML.PROJECT, MENUactionINTERN); break; 
				case "OPTION": MENUintern.readXML(MENUXML.OPTION, MENUactionINTERN); break; 
				case "ABOUT": MENUintern.readXML(MENUXML.ABOUT, MENUactionINTERN); break; 
			}
			CreateMenu()
		}
		private function MENUactionINTERN(t:String):void
		{
			switch (t){
				case "RESET": trace("reset");RESETFLASH(); break; 
				case "FPS": trace("reset");FPS(); break; 
				// ?
				case "CONTACT": trace("mail"); break; 
				case "FORUM": trace("message"); break; 
				case "BLOG":  DLINK('http://3dflashlo.wordpress.com/'); break;
				case "SOURCE":DLINK('http://code.google.com/p/3dflashlo/'); break; 
				// project menu
				case "PRO1": GETFLASH(0);break; 
				case "PRO2":  GETFLASH(1);break; 
				case "PRO3":  GETFLASH(2);break; 
				case "PRO4":  GETFLASH(3);break; 
				// display menu
				case "DISPLAY1": Resolution(1); break; 
				case "DISPLAY2": Resolution(2); break; 
				case "DISPLAY3": Resolution(3); break; 
				case "DISPLAY4": Resolution(4); break; 
				// option panel
				case "VIEW": PANNEL.ADD(1); break; 
				case "LIGHT": PANNEL.ADD(2);  break; 
				case "MATERIAL": PANNEL.ADD(3); break;
				case "PHYSIC": PANNEL.ADD(4); break; 
			}
			hideMENU()
		}
		// _______________________________project files
		private function GETFLASH(n:int):void
		{
			var name:String
			switch (n){
				case 0:name= "main"; break;
				case 1:name= "baseJiglib"; break;
				case 2:name= "baseBullet"; break;
				case 3:name= "bulletlabs"; break;
			}
			if(ID!=-1){ExternalInterface.call("LOAD", ID, name ); }
			//else {if(name=="main")name="index"; DLINK("http://3dflashlo.free.fr/"+name+".html");}
		}
		
		private function DLINK(link:String):void
		{
			navigateToURL ( new URLRequest( link ) ,"_self" );
		}
		private function LINK(link:String):void
		{
			navigateToURL ( new URLRequest( link ) ,"_blank" );
		}
		
		private function FPS():void
		{
			if(S==null) {S=new SYS_stats( BASE, FONT.CSS);coinBas.addChild(S); MODE(S);Plusnum=2 }
			else{ S.destroy(); coinBas.removeChild(S);S=null; Plusnum=1; }
		}
		
		// ____________________________________ MENU 
		private function CreateMenu():void
		{
			BASE.MenuActif=true
			mousefake=new Sprite()
			mousefake.y=0; 	
			var cu:Shape=BASE.TOOL.CARRE({color:0x00, alpha:0, r:0})
			var py:int=30//35
			mousefake.addChild(cu)
			newMenu=new Sprite()
			var bg:Shape=BASE.TOOL.CARRE({r:0, color:0x000000, alpha:.6, w:MENUintern.getWidth()+10, h:MENUintern.getHeight()+16, x:-15, y:py-3})
			MENUintern.x=-10; MENUintern.y=py+5;
			newMenu.x=parent.stage.mouseX;
			mousefake.x=parent.stage.mouseX; 
			newMenu.addChild(bg);
			newMenu.addChild(MENUintern);
			MENUclip.addChild(mousefake);
			MENUclip.addChild(newMenu);
			
			parent.stage.addEventListener("mouseMove", testeMouse)
			mousefake.addEventListener("click", hideMENU)
		}
		private function testeMouse(e:MouseEvent=null):void
		{
			mousefake.x=parent.stage.mouseX-5
			if(parent.stage.mouseY>22)mousefake.y=parent.stage.mouseY-5
			else mousefake.y=0
		}
		private function hideMENU(e:MouseEvent=null):void
		{
			BASE.MenuActif=false;
			while(newMenu.numChildren) newMenu.removeChildAt(0); 
			while(mousefake.numChildren) mousefake.removeChildAt(0); 
			parent.stage.removeEventListener("mouseMove", testeMouse);
			mousefake.removeEventListener("click", hideMENU);
			MENUclip.removeChild(mousefake);
			MENUclip.removeChild(newMenu);
			mousefake=null;
			newMenu=null; 
		}
		private function RESETFLASH():void
		{
			if(ID!=-1)ExternalInterface.call("RELOAD", ID );
		}
		public function Resolution (n:int):void
		{ 
			if (Display!=n){
				switch (n){
					case 1:ZHTML(true); Display=1; break;
					case 2:ZHTML(false); Display=2; break;
					case 3:ZFULL(false); Display=3; break;
					case 4:ZFULL(true); Display=4; break;
		}}}
		private function ZHTML(limite:Boolean):void
		{ 
			if(limite) { if(ID!=-1)ExternalInterface.call("RESTRICT" );BASE.limite=true;}
			else{ if(ID!=-1)ExternalInterface.call("LIMITE","limit", 960,450 );BASE.limite=false;BASE.activeResize()}
			FullScreen(false);
		}
		private function ZFULL(zoom:Boolean):void
		{ 
			BASE.limite=false;
			var z:Number=1;
			if(zoom)z=.5;
			parent.stage.fullScreenSourceRect = new Rectangle(0, 0, parent.stage.fullScreenWidth*z, parent.stage.fullScreenHeight*z);
			FullScreen(true);
		}
		private function FullScreen(B:Boolean):void{with(parent.stage)
		{
			if(!B) displayState = "normal";
			else  displayState = "fullScreen";
		}
			BASE.RESIZE();
		}
		
		// resize Interface
		public function cadre(P:Point):void 
		{ 
			if(BUG) BUG.Resize();
			if(viseur){viseur.x=P.x>>1; viseur.y=P.y>>1;} 
			clipBas.y=P.y-40;
			coinBas.x=P.x; coinBas.y=P.y;
		}
				
		private var colors:Array=[0x323436,0x252324,0x353334,0x323436];
		private var S:SYS_stats;
		public var ID:int=-1;
		private var content:Sprite;
		private var coinBas:Sprite;
		private var clipBas:Sprite;
		private var Inter:Array;
		private var panel:Sprite;
		private var MENUclip:Sprite;
		private var newMenu:Sprite;
		private var mousefake:Sprite;
		// visor fps
		public var viseur:Sprite;
		private var cible:MovieClip;
		private var ZONE:MovieClip;
		private var LOADERCLIP:MovieClip=null;
		// text
		private var SORTIEtext:SYS_text;
		private var MENUintern:SYS_text;
		private var MENUtext:SYS_text;
		private var MessagePlay:SYS_text;
		private var PANNEL:SYS_pannel;
		// font & css
		public var FONT:SYS_fonts;
		// menu xml direct
		private var MENUXML:SYS_xml;
		// screen resolution/position
		private var Display:int=1;
		private var Plusnum:int=1;
		private var FIN:Function;
		public var actif:Boolean=false;
		
		public var MESSAGE:String;
		private var def:Object;
		private var BASE:Object;
		public var BUG:PlayerError;
	}}